package org.zhengquan.service;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zhengquan.common.RequestHolder;
import org.zhengquan.dao.SysRoleAclMapper;
import org.zhengquan.dao.SysRoleMapper;
import org.zhengquan.dao.SysRoleUserMapper;
import org.zhengquan.exception.ParamException;
import org.zhengquan.pojo.SysRole;
import org.zhengquan.util.BeanValidator;
import org.zhengquan.util.IpUtil;
import org.zhengquan.vo.RoleVO;

import java.util.Date;
import java.util.List;

/**
 * @author zhengquan
 * @date 2017/11/3
 */
@Service
public class SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;

    @Autowired
    private SysRoleAclMapper sysRoleAclMapper;

    @Autowired
    private SysLogService sysLogService;

    public void save(RoleVO roleVO) {
        BeanValidator.check(roleVO);
        if (checkExit(roleVO.getId(), roleVO.getName())) {
            throw new ParamException("角色名称已占用");
        }
        SysRole sysRole = SysRole.builder().name(roleVO.getName()).type(roleVO.getType())
                .state(roleVO.getState()).remark(roleVO.getRemark()).build();
        sysRole.setOperator(RequestHolder.getCurrentUser().getUsername());
        sysRole.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
        sysRole.setOperateTime(new Date());
        sysRoleMapper.insertSelective(sysRole);
        sysLogService.saveRoleLog(null,sysRole);
    }

    public void update(RoleVO roleVO) {
        BeanValidator.check(roleVO);
        if (checkExit(roleVO.getId(), roleVO.getName())) {
            throw new ParamException("角色名称已占用");
        }
        SysRole before = sysRoleMapper.selectByPrimaryKey(roleVO.getId());
        Preconditions.checkNotNull(before, "待更新的角色不存在");

        SysRole after = SysRole.builder().id(roleVO.getId()).name(roleVO.getName()).type(roleVO.getType())
                .state(roleVO.getState()).remark(roleVO.getRemark()).build();
        after.setOperator(RequestHolder.getCurrentUser().getUsername());
        after.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
        after.setOperateTime(new Date());
        sysRoleMapper.updateByPrimaryKeySelective(after);
        sysLogService.saveRoleLog(before,after);
    }

    public List<SysRole> selectList() {
        return sysRoleMapper.selectList();
    }


    public List<SysRole> selectRoleListByUserId(Integer userId) {
        List<Integer> roleIdList = sysRoleUserMapper.selectRoleIdListByUserId(userId);
        if (CollectionUtils.isEmpty(roleIdList)) {
            return Lists.newArrayList();
        }
        return sysRoleMapper.selectListByRoleIdList(roleIdList);
    }

    public List<SysRole> selectRoleListByAclId(Integer aclId) {
        List<Integer> roleIdList = sysRoleAclMapper.selectRoleIdListByAclId(aclId);
        if (CollectionUtils.isEmpty(roleIdList)) {
            return Lists.newArrayList();
        }
        return sysRoleMapper.selectListByRoleIdList(roleIdList);
    }

    private boolean checkExit(Integer roleId, String roleName) {
        return sysRoleMapper.countByNameAndRoleId(roleName, roleId) > 0;
    }
}
